#ifndef DBWORK_H
#define DBWORK_H

#include <QString>
#include <QtSql>
#include <QDebug>
#include <QVariantList>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <boost/shared_ptr.hpp>

namespace DatabaseModule {

	//typedef boost::shared_ptr<QSqlQueryModel> QueryModelPtr;
	//typedef boost::shared_ptr<QSqlTableModel> TableModelPtr;
	typedef QSqlQueryModel* QueryModelPtr;
	typedef QSqlTableModel* TableModelPtr;
	typedef boost::shared_ptr<QSqlQuery> SqlQueryPtr;

	class DatabaseWork {
		
	public:

		DatabaseWork() : dbName(QString("")), db(QSqlDatabase()), inTransaction(false) {}
		DatabaseWork(const QString& name) : dbName(name), db(QSqlDatabase()), inTransaction(false) {}
		virtual ~DatabaseWork() {destroyConnection();}

		void setDbName(const QString &name) {dbName = name;}
		QString getDbName() {return dbName;}

		bool createConnection();
		void destroyConnection() {db.close();}

		bool execSqlQuery(const QString& query);
		bool execSqlQuery(const QString& query, QSqlQuery& result);
		QueryModelPtr getSqlModel(const QString& query);
		TableModelPtr getTableModel(const QString& tableName);		
		
	private:

		QString dbName;
		QSqlDatabase db;
		bool inTransaction;
		SqlQueryPtr transactionPtr;


		//unused
		void initTransaction();
		void setTransactionTemplate(const QString& templ);
		bool addTransactionValues(const QVariantList& values);
		bool execTransaction();
		void clearTransaction();
	
	};

}

#endif //DBWORK_H